const Debounce = require("../template")

function testDebounceAdvancedMethods() {
  console.log(
    "1. 防抖函数参数传递测试:",
    (() => {
      let result = null
      const debounced = Debounce.debounce((a, b, c) => {
        result = a + b + c
      }, 100)
      
      debounced(1, 2, 3)
      debounced(4, 5, 6)
      
      return result === null // 防抖期间不应该执行
    })()
  )

  console.log(
    "2. 防抖函数返回值测试:",
    (() => {
      const debounced = Debounce.debounce(() => {
        return 'test'
      }, 100)
      
      const result = debounced()
      return result === undefined // 防抖期间返回undefined
    })()
  )

  console.log(
    "3. 防抖函数this绑定测试:",
    (() => {
      const obj = {
        value: 42,
        getValue: Debounce.debounce(function() {
          return this.value
        }, 100)
      }
      
      const result = obj.getValue()
      return result === undefined // 防抖期间返回undefined
    })()
  )

  console.log(
    "4. 防抖函数多次调用测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      for (let i = 0; i < 10; i++) {
        debounced()
      }
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "5. 防抖函数延迟时间测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 50)
      
      debounced()
      debounced()
      
      return callCount === 0 // 短延迟期间不应该调用
    })()
  )

  console.log(
    "6. 防抖函数选项配置测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100, { leading: true, trailing: false })
      
      debounced()
      debounced()
      
      return callCount === 1 // leading为true应该立即调用一次
    })()
  )

  console.log(
    "7. 防抖函数取消后重新调用测试:",
    (() => {
      let callCount = 0
      const { debounced, cancel } = Debounce.debounceWithCancel(() => {
        callCount++
      }, 100)
      
      debounced()
      cancel()
      debounced()
      
      return callCount === 0 // 取消后重新调用不应该执行
    })()
  )

  console.log(
    "8. 防抖函数刷新后状态测试:",
    (() => {
      let callCount = 0
      const { debounced, flush, isPending } = Debounce.debounceWithFlush(() => {
        callCount++
      }, 100)
      
      debounced()
      flush()
      
      return callCount === 1 && isPending() === false
    })()
  )

  console.log(
    "9. 防抖函数状态查询测试:",
    (() => {
      const { debounced, isPending, isCancelled } = Debounce.debounceWithState(() => {}, 100)
      
      debounced()
      
      return isPending() === true && isCancelled() === false
    })()
  )

  console.log(
    "10. 防抖函数条件执行测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithCondition(() => {
        callCount++
      }, 100, (args) => args[0] > 5)
      
      debounced(3)
      debounced(7)
      debounced(2)
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "11. 防抖函数优先级测试:",
    (() => {
      let callCount = 0
      const highPriority = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriority = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriority()
      highPriority()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "12. 防抖函数批量执行测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithBatch(() => {
        callCount++
      }, 100, 3)
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "13. 防抖函数错误处理测试:",
    (() => {
      let errorCaught = false
      const debounced = Debounce.debounceWithErrorHandling(() => {
        throw new Error('test error')
      }, 100, (error) => {
        errorCaught = true
      })
      
      debounced()
      
      return errorCaught === false // 防抖期间不应该执行错误处理
    })()
  )

  console.log(
    "14. 防抖函数进度回调测试:",
    (() => {
      let progressCalled = false
      const debounced = Debounce.debounceWithProgress(() => {}, 100, (progress) => {
        progressCalled = true
      })
      
      debounced()
      
      return progressCalled === false // 防抖期间不应该调用进度回调
    })()
  )

  console.log(
    "15. 防抖函数统计信息测试:",
    (() => {
      const { debounced, getStats } = Debounce.debounceWithStats(() => {}, 100)
      
      debounced()
      debounced()
      
      const stats = getStats()
      return typeof stats === 'object' && stats.callCount === 0
    })()
  )
}

testDebounceAdvancedMethods()
